package fun.xinnix.controller;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.log.StaticLog;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.*;
import fun.xinnix.entity.Result;
import fun.xinnix.service.TestService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;


@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private TestService testService;

    @PostMapping("/testUploadFile")
    public Result uploadFile(MultipartFile file) throws IOException {
        String endpoint = "oss-cn-shenzhen.aliyuncs.com";
        String accessKeyId = "LTAI5t8731EUjTUxQrNTpdSg";
        String accessKeySecret = "VnvOLA51jO3sZ94ehqg6abZSKgnBnq";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try (InputStream inputStream = file.getInputStream()) {
            long fileSize = file.getSize();
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(fileSize); // 文件大小
            String fileType = FileNameUtil.extName(file.getOriginalFilename());
            String fileName = IdUtil.fastSimpleUUID() + "." + fileType;
            PutObjectRequest putObjectRequest = new PutObjectRequest("zlxss-prd", fileName, inputStream, metadata);
            ossClient.putObject(putObjectRequest);
            ossClient.shutdown();
            String imgUrl = "https://zlxss-prd.oss-cn-shenzhen.aliyuncs.com/" + fileName;
            return Result.success(JSONObject.of("url", imgUrl));
        }
    }

    @PostMapping("/convertPdf")
    public void convertPdf(MultipartFile file) {
        testService.convertPdf(file);
    }

    @GetMapping("/testLogAdvice")
    public Result testLogAdvice() {
        testService.testLogAdvice();
        return Result.success();
    }

    @GetMapping("/testLogAdviceAndResult/id/{id}")
    public Result testLogAdviceAndResult(@PathVariable Long id, String name) {
        return Result.success(testService.testLogAdviceResult(id, name));
    }
}
